<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$selectUserArr   = array();  
	$selectUserAjArr = array();
	$filterUserId    = isset($_GET['filterUserId']) ? $_GET['filterUserId'] : 0;
	if(isset($filterUserId))
	{
	  $selectUserAj = "SELECT user.userId,user.userName,usertarget.usertargetId,DATE_FORMAT(usertarget.month,'%b-%Y') AS month,
	                          usertarget.target,usertarget.achievedTarget
	                     FROM usertarget
	                     JOIN user
	                    WHERE user.userId = usertarget.userId";
	  if($filterUserId > 0)
	  {
	    $selectUserAj .= " AND usertarget.userId = ".$_GET['filterUserId'];
	  }
	  if(isset($_GET['monthMonth']) && $_GET['monthMonth'] != '')
	  {
	    $selectUserAj .= " AND usertarget.month = '".$_GET['monthYear']."-".$_GET['monthMonth']."-01'";
	  }    
	  $selectUserAjRes = mysql_query($selectUserAj);
	  $selectUserAjCnt = 0;
	  while($userAjRow = mysql_fetch_array($selectUserAjRes))
	  {
	  	$selectUserAjArr[$selectUserAjCnt]['userName']        = $userAjRow['userName'];
	  	$selectUserAjArr[$selectUserAjCnt]['month']           = $userAjRow['month'];
	  	$selectUserAjArr[$selectUserAjCnt]['target']          = $userAjRow['target'];
	  	$selectUserAjArr[$selectUserAjCnt]['achievedTarget']  = $userAjRow['achievedTarget'];
	  	$selectUserAjArr[$selectUserAjCnt]['balanceTarget']   = $userAjRow['target'] - $userAjRow['achievedTarget'];
  	  $selectUserDetail = "SELECT party.companyName,party.partyId
  	                              usertargetachieved.userTargetId
                             FROM party
                             JOIN usertargetachieved ON party.partyId = usertargetachieved.partyId
                             WHERE usertargetachieved.userTargetId=".$userAjRow['usertargetId'];
      $selectUserDetailRes = mysql_query($selectUserDetail); 
      //$selectUserCnt = 0;  
      //while($userRow = mysql_fetch_array($selectUserDetailRes))
      //{
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['companyName'] = $userRow['companyName'];
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['orderValue']  = $userRow['orderValue'];
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['orderDate']   = $userRow['orderDate'];
      //	$selectUserCnt++;
      //}
      //if($selectUserCnt == 0)
      //{
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['companyName'] = "--";
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['orderValue']  = "--";
      //	$selectUserArr[$selectUserAjCnt][$selectUserCnt]['orderDate']   = "--";
      //}
	    $selectUserAjCnt++; 
	  }  
	  $smarty->assign("selectUserArr",$selectUserArr);     
	  $smarty->assign("selectUserAjArr",$selectUserAjArr); 
	  $smarty->display("userTargetAj.tpl");
  }
}
?>